package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.SystemRoleDO;
import com.parenting.mapper.SystemRoleMapper;
import com.parenting.request.AddRoleRequest;
import com.parenting.request.PageRoleListRequest;
import com.parenting.request.UpdateRoleRequest;
import com.parenting.service.SystemRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-13
 */
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRoleDO> implements SystemRoleService {

    @Override
    public void saveRole(AddRoleRequest role) {
        SystemRoleDO systemRoleDO = BeanUtil.copyProperties(role, SystemRoleDO.class);
        Date date = new Date();
        systemRoleDO.setUpdateDate(date);
        systemRoleDO.setCreateDate(date);
        baseMapper.insert(systemRoleDO);
    }

    @Override
    public void updateRole(UpdateRoleRequest role) {
        SystemRoleDO systemRoleDO = BeanUtil.copyProperties(role, SystemRoleDO.class);
        Date date = new Date();
        systemRoleDO.setUpdateDate(date);
        systemRoleDO.setCreateDate(date);
        baseMapper.updateById(systemRoleDO);
    }

    @Override
    public IPage<SystemRoleDO> pageRole(PageRoleListRequest page) {
        LambdaQueryWrapper<SystemRoleDO> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(page.getRoleName())){
            wrapper.like(SystemRoleDO::getRoleName,page.getRoleName());
        }
        IPage<SystemRoleDO> systemRoleDOIPage = baseMapper.selectPage(page.getPage(), wrapper);
        return systemRoleDOIPage;
    }


    @Override
    public SystemRoleDO getRoleById(String id) {

        return baseMapper.selectById(id);
    }


    @Override
    public void deleteRoleById(String roleId) {

        baseMapper.deleteById(roleId);

    }

    @Override
    public List<SystemRoleDO> getListRole(String shopId) {
        return   baseMapper.selectList(new LambdaQueryWrapper<SystemRoleDO>().eq(SystemRoleDO::getShopId,shopId));
    }


}
